package com.jiazhong.basic.shuxin.design.单例模式;

import lombok.SneakyThrows;

import java.lang.reflect.Constructor;
import java.util.concurrent.atomic.AtomicReference;

public class App {

    private static void a() {
        DemoA a1 = DemoA.getInstance();
        DemoA a2 = DemoA.getInstance();
        System.out.println(a1 == a2);
        System.out.println(a1);
        System.out.println(a2);
    }

    private static void b1() {
        DemoB a1 = DemoB.getInstance();
        DemoB a2 = DemoB.getInstance();
        System.out.println(a1 == a2);
        System.out.println(a1);
        System.out.println(a2);
    }

    @SneakyThrows
    private static void b2() {
        AtomicReference<DemoB> b1 = new AtomicReference<>(), b2 = new AtomicReference<>();
        Thread t1 = new Thread(() -> {
            b1.set(DemoB.getInstance());
        });
        Thread t2 = new Thread(() -> {
            b2.set(DemoB.getInstance());
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(b1.get());
        System.out.println(b2.get());
        System.out.println(b1.get() == b2.get());
    }

    @SneakyThrows
    private static void c1() {
        Class<?> forName = Class.forName("com.jiazhong.basic.shuxin.design.单例模式.DemoA");
        Constructor<?> constructor = forName.getDeclaredConstructor();
        // 设置访问私有内容
        constructor.setAccessible(true);
        DemoA a1 = (DemoA) constructor.newInstance();
        DemoA a2 = (DemoA) constructor.newInstance();
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a1 == a2);
    }

    private static void c2() {
        DemoE e1 = DemoE.INSTANCE;
        DemoE e2 = DemoE.INSTANCE;
        System.out.println(e1 == e2);
        System.out.println(e1);
        System.out.println(e2);
        e1.a();
        e1.b();
        e2.a();
        e2.b();
    }
    public static void main(String[] args) {
        c2();
    }
}
